package com.xxxx.controller;

import com.xxxx.pojo.Door;
import com.xxxx.service.DoorService;
import com.xxxx.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created By ShaoXikai on 2022/7/23
 */
@Controller
@RequestMapping("/door")
public class DoorController {

    @Autowired
    @Qualifier("doorServiceImpl")
    private DoorService doorService;

    @Autowired
    @Qualifier("orderServiceImpl")
    private OrderService orderService;

    @GetMapping("/list")
    public String list(Model model) {
        model.addAttribute("doors", doorService.findAll());
        return "door_list";
    }

    @GetMapping("/toSave")
    public String toSave() {
        return "door_add";
    }

    @PostMapping("/save")
    public String save(Door door) {
        doorService.save(door);
        return "redirect:/door/list";
    }

    @GetMapping("/toUpdate/{id}")
    public String toUpdate(@PathVariable long id, Model model) {
        Door door = doorService.findById(id);
        model.addAttribute("door", door);
        return "door_update";
    }

    @PostMapping("/update")
    public String update(Door door) {
        System.out.println(door);
        doorService.modify(door);
        return "redirect:/door/list";
    }

    @GetMapping("/remove/{id}")
    public String remove(@PathVariable Long id) {
        orderService.removeByDoorId(id);
        doorService.remove(id);
        return "redirect:/door/list";
    }

}
